﻿<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
		Save Attachments from Outlook E-Mail Items
	  </Title>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[
		private void ThisApplication_Startup(object sender, System.EventArgs e)
		{
			this.NewMail += new Microsoft.Office.Interop.Outlook
				.ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMail);
		}

		private void ThisApplication_NewMail()
		{
			Outlook.MAPIFolder inBox = this.ActiveExplorer()
				.Session.GetDefaultFolder(Outlook
				.OlDefaultFolders.olFolderInbox);
			Outlook.Items inBoxItems = inBox.Items;
			Outlook.MailItem newEmail = null;
			inBoxItems = inBoxItems.Restrict("[Unread] = true");
			try
			{
				foreach (object collectionItem in inBoxItems)
				{
					newEmail = collectionItem as Outlook.MailItem;
					if (newEmail != null)
					{
						if (newEmail.Attachments.Count > 0)
						{
							for (int i = 1; i <= newEmail
							   .Attachments.Count; i++)
							{
								newEmail.Attachments[i].SaveAsFile
									(@"C:\TestFileSave\" +
									newEmail.Attachments[i].FileName);
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				string errorInfo = (string)ex.Message
					.Substring(0, 11);
				if (errorInfo == "Cannot save")
				{
					MessageBox.Show(@"Create Folder C:\TestFileSave");
				}
			}
		}

		]]>
	</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>